﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.UI.Common.Events;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
	public class PersonPresenter
	{
		private PersonDO _Person;
		private PersonView _View;
        private readonly IEventAggregator _EventAggregator;
		private SubscriptionToken _SubscriptionToken;

		public PersonPresenter(IEventAggregator eventAggregator)
        {
            this._EventAggregator = eventAggregator;
        }

		public PersonView View
		{
			get { return _View; }
			set { _View = value; }
		}

		private void PersonSelectedHandler(PersonDO person)
		{
			this._Person = person;
			this._View.Person = this._Person;
		}

        public void SubscribePersonSelectedEvent(PersonDO person)
        {
            this._Person = person;
            PersonSelectedEvent personSelectedEvent =
                _EventAggregator.GetEvent<PersonSelectedEvent>();
            if (this._SubscriptionToken != null)
            {
                personSelectedEvent.Unsubscribe(_SubscriptionToken);
            }
            this._SubscriptionToken = personSelectedEvent.Subscribe(
                PersonSelectedHandler,
                ThreadOption.UIThread,
                false);
        }
	}
}
